<template>
  <div class="app-container">
    <template>
      <!--查询表单-->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="teacherQuery.name" placeholder="姓名" />
        </el-form-item>

        <el-form-item>
          <el-select v-model="teacherQuery.level" clearable placeholder="教师头衔">
            <el-option :value="1" label="大佬" />
            <el-option :value="2" label="究极大佬" />
          </el-select>
        </el-form-item>

        <el-form-item label="添加时间">
          <el-date-picker v-model="teacherQuery.begin" type="datetime" placeholder="开始时间" value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00" />
        </el-form-item>
        <el-form-item>
          <el-date-picker v-model="teacherQuery.end" type="datetime" placeholder="截止时间" value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00" />
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="getTeacherListPage()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form>

      <!-- 表单 -->
      <el-table ref="multipleTable" :data="list" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="序号" width="50" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="name" label="姓名" width="120"> -->
        <el-table-column prop="name" label="姓名">
        </el-table-column>
        <el-table-column prop="intro" label="简介" width="300" show-overflow-tooltip>
        </el-table-column>
        <!-- <el-table-column label="头衔" width="100" > -->
        <el-table-column label="头衔">
          <template slot-scope="scope">
            {{scope.row.level===1?'大佬':'究极大佬'}}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="sort" label="排序" width="100" /> -->
        <el-table-column prop="sort" label="排序" width="100" />
        <el-table-column prop="gmtCreate" label="创建时间"></el-table-column>
        <!-- <el-table-column label="操作" width="200" align="center"> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <router-link :to="'/teacher/edit/'+scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
            </router-link>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeTeacherById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination :current-page="page" :page-size="limit" :total="total" style="padding: 30px 0; text-align: center;"
        layout="total, prev, pager, next, jumper" @current-change="getTeacherListPage" />
    </template>
  </div>
</template>


<script>
  // 引入 js 文件，后编写核心代码
  import teacher from '@/api/edu/teacher'

  export default {
    // data: {
    // },

    data() {
      return {
        list: null, //查询之后返回接口集合
        page: 1, //当前页数
        limit: 10, //每页记录数
        total: 0, //总记录数
        teacherQuery: {} //条件封装对象
      }
    },
    created() {
      this.getTeacherListPage()
    },
    methods: {
      getTeacherListPage(page = 1) {
        this.page = page
        teacher.getTeacherListPage(this.page, this.limit, this.teacherQuery)
          .then(response => { //请求成功
            // response 接口返回的数据
            console.log(response.data.rows)
            this.list = response.data.rows
            this.total = response.data.total
          })
      },
      // 删除教师
      removeTeacherById(id) {
        const h = this.$createElement;
        // this.$confirm('此操作将删除教师, 是否继续?', '警告', {
        this.$confirm(h('h2', {style: 'color: teal'}, '此操作将删除教师, 是否继续?'), '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => { // 点击确定执行 then 方法
          teacher.removeTeacherById(id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getTeacherListPage()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },

      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      resetData() {
        this.teacherQuery = {}
        this.getTeacherListPage()
      }
    }
  }
</script>
